<template>
  <div class="goods-container" >
    <div v-if="palteTabList.length > 0" class="header-row">
      <h2 class="section-title">{{views[0].name }}</h2>
      <div class="tag-list">
        <a 
          v-for="tab in palteTabList" 
          :key="tab.id" 
          :href="tab.jumpUrl"
          class="tag-item"
        >
          {{ tab.name }}
        </a>
      </div>
    </div>

    <div v-if="goodsList.length > 0" class="goods-grid">
      <div v-for="(item) in goodsList.slice(0, 8)" 
      :key="item.id" 
      class="goods-item"
      @click.stop="goToGoodsDetail(item.goodsId)"
      >
        <div class="image-container" @mouseover="hoverImage = item.id" @mouseleave="hoverImage = null"  @click.stop>
          <img 
            :src="item.imgUrl" 
            :alt="item.goodsName" 
            class="goods-image"
            :class="{'image-hover': hoverImage === item.id}"
          >
          <span v-if="item.isNew === 1" class="new-badge">新品</span>
        </div>
        <div class="goods-info">
          <h3 class="goods-name">{{ item.goodsName }}</h3>
          <p class="goods-price">¥{{ item.price.toLocaleString() }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getPCPlateContent } from "@/api";

export default {
  name: "Recommend",
  data() {
    return {
      goodsList: [],
      hoverImage: null,
      palteTabList:[],
      views:[]
    };
  },
methods: {
  goToGoodsDetail(goodsId) {
    this.$router.push({
      name: 'GoodsDetail',
      params: { 
        id: goodsId 
      }
    }).catch(err => {
      if (!err.message.includes('Avoided redundant navigation')) {
        console.error('路由跳转错误:', err);
      }
    });
  }
}
    ,
  async created() {
    try {
      const response = await getPCPlateContent();
      this.views=response.result.views;
      this.goodsList = response.result.views[0].goodsList;
      this.palteTabList = response.result.views[0].palteTabList;
    } catch (error) {
      console.error('失败', error);
    }
  }
};
</script>

<style scoped>
.header-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding: 0 10px;
}

.section-title {
  font-size: 22px;
  font-weight: bold;
  color: #333;
  margin: 0;
}

.tag-list {
  display: flex;
  gap: 15px;
}

.tag-item {
  color: #666;
  text-decoration: none;
  font-size: 14px;
  transition: all 0.3s;
  padding: 5px 0;
  position: relative;
}

.tag-item:hover {
  color: #1890ff;
}

.tag-item:not(:last-child)::after {
  content: "/";
  position: absolute;
  right: -10px;
  color: #ddd;
}

.goods-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.goods-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
}

.goods-item {
  border: 1px solid #eee;
  border-radius: 8px;
  overflow: hidden;
  background: white;
}

.image-container {
  position: relative;
  width: 100%;
  height: 0;
  padding-bottom: 100%; 
  overflow: hidden;
}

.goods-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: contain; 
  transition: transform 0.5s ease;
}

.image-hover {
  transform: scale(1.05);
}

.goods-info {
  padding: 15px;
  text-align: left;
}

.goods-name {
  font-size: 14px;
  margin: 0 0 8px 0;
  line-height: 1.4;
  height: 2.8em;
  overflow: hidden;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  text-overflow: ellipsis;
}


.goods-price {
  color: #f56c6c;
  font-size: 16px;
  font-weight: bold;
  margin: 0;
  text-align: left; 
}

.new-badge {
  position: absolute;
  top: 10px;
  left: 10px;
  background-color: #ff4d4f;
  color: white;
  padding: 3px 8px;
  font-size: 12px;
  font-weight: bold;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  z-index: 1;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
  100% {
    transform: scale(1);
  }
}
</style>